package operation;

import JDBC.JDBCUtils;
import Medicine.Pharmacy;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;


public class AddAmountOfEqu implements IOPeration{
    @Override
    public void work(Pharmacy pharmacy) throws SQLException {
        //创建QueryRunner对象
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        System.out.println("增加器械库存....");
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入您想增加库存的器械名称: ");
        String name = sc.nextLine();

        String query = "SELECT Amount FROM Equipment WHERE Name=?";
        List<Map<String, Object>> equipmentList = qr.query(query, new MapListHandler(), name);
        int amount = 0;
        if (!equipmentList.isEmpty() && equipmentList.get(0).containsKey("Amount")) {
            amount = (Integer) equipmentList.get(0).get("Amount");
        }

        if (amount >= 0) {
            System.out.println("药店中有该器械");
            System.out.println("请输入添加的数量！");
            int num = sc.nextInt();

            String updateSql = "UPDATE Equipment SET Amount=Amount+? WHERE Name=?";
            Object[] updateParms = {num, name};
            int count = qr.update(updateSql, updateParms);
            if (count < 1) {
                System.out.println("添加器械数量失败！");
            } else {
                System.out.println("添加器械数量成功！");
            }
        }
    }
}

